﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ChatForum.Domain
{
    internal class ForumLogic
    {
        public static void CreateCategory(Category category, IRepository repository)
        {
            //Get all Category from database
            var commoncat = repository.GetAll<Category>().Where(c => c.Name.Equals(category.Name, StringComparison.CurrentCultureIgnoreCase));
            if (commoncat != null && commoncat.Count() > 0)
                throw new DuplicateCategoryException();

            repository.Insert<Category>(category);
        }
        public static void CreatePost(Post post, IRepository _repository)
        {
            //Check this Category Id is really exist in database or not
            Category category = _repository.GetById<Category>(post.ParentCategory);
            if (category == null)
                throw new InvalidCategoryException();

            _repository.Insert<Post>(post);
        }

        public static IEnumerable<Category> GetAllCategory(IRepository repository)
        {
            return repository.GetAll<Category>();
        }

        public static Category GetCategoryById(Guid catId, IRepository repository)
        {
            return repository.GetById<Category>(catId);
        }

        public static IEnumerable<Post> GetPostbyCategory(Guid categoryId, bool includeChildren, IRepository repository)
        {
            var allPost = repository.GetAll<Post>().Where(p => p.ParentCategory == categoryId);
            return includeChildren ? allPost : allPost.Where(p => p.ParentPost == Guid.Empty);
        }

        public static IEnumerable<Post> GetPostbyCreator(string userId, bool includeChildren, IRepository repository)
        {
            var allPost = repository.GetAll<Post>().Where(p => p.CreatedBy == userId);
            return includeChildren ? allPost : allPost.Where(p => p.ParentPost == Guid.Empty);
        }

        public static IEnumerable<Post> GetPostbyParentPost(Guid parentPostid, bool includeChildren, IRepository repository)
        {
            var allPost = repository.GetAll<Post>().Where(p => p.ParentPost == parentPostid);
            return includeChildren ? allPost : allPost.Where(p => p.ParentPost == Guid.Empty);
        }
        public static IEnumerable<Post> GetThread(IRepository repository, Guid PostId)
        {
            List<Post> fullTreeList = new List<Post>();

            //Get the top level Post from DB, i.e. P1, and add that into the fullTreeList
            fullTreeList.Add(repository.GetById<Post>(PostId));

            List<Post> directChildPostList = repository.GetAll<Post>().Where(p => p.ParentPost == PostId).ToList();

            foreach (Post directChildPost in directChildPostList)
            {
                List<Post> childPostList = repository.GetAll<Post>().Where(p => p.ParentPost == directChildPost.ID).ToList();
                if (childPostList != null && childPostList.Count > 0)
                    fullTreeList.AddRange(GetThread(repository, directChildPost.ID));
                else
                    fullTreeList.Add(directChildPost);

            }
            return fullTreeList;           
        }


        public static Post GetPostById(Guid postId, IRepository repository)
        {
            return repository.GetById<Post>(postId);
        }

		public static void ModifyCategory(Category category, IRepository repository)
		{
			repository.Modify<Category>(category);
		}
	}
}
